package com.example.demo.controller;

import com.example.demo.model.UserModer;
import com.example.demo.service.MySQLUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Scope("prototype")
@RequestMapping("/user")
public class UserLogin {
  @Autowired
  private MySQLUserService mySQLUserService;

  @PostMapping(value = "/select")
  public List<UserModer> select() throws Exception {
    return mySQLUserService.select();
  }

  @PostMapping(value = "/login")
  public String login(@RequestBody UserModer userModer) throws Exception {
    if (mySQLUserService.checkUser(userModer.getName(), userModer.getPassword()).isEmpty()) {
      return "登录失败";
    } else return "登录成功";
  }

  @PostMapping(value = "/signup")
  public int insert(@RequestParam(value = "name") String name) throws Exception {
    return mySQLUserService.insert(name);
  }
}
